/*
 * Created on Jul 19, 2005
 *
 */
package com.erclab.internal.xpresso.unittests;

import java.util.*;

import junit.framework.TestCase;

import com.erclab.internal.xpresso.*;
import com.erclab.internal.xpresso.forms.*;
import com.erclab.internal.xpresso.formelements.*;
import com.erclab.internal.xpresso.reports.*;
import com.erclab.internal.xpresso.reportelements.*;
import com.erclab.internal.xpresso.security.User;
import com.erclab.internal.xpresso.conf.Settings;
import com.erclab.internal.xpresso.dataAccess.DataModifier;
import com.erclab.internal.xpresso.datavalidators.DataValidationError;
import com.erclab.internal.xpresso.exceptions.ApplicationException;
import com.erclab.internal.xpresso.exceptions.DataModificationException;
import com.erclab.internal.xpresso.exceptions.DirectoryException;
import com.erclab.internal.xpresso.exceptions.FormException;

/**
 * @author Enrique Rodriguez.
 *
 */
public class MultiSelectTest extends TestCase {

	Application grad = null;
	User user = null;
	
	protected void setUp() throws Exception {
		super.setUp();
		try {
			grad = new Application();
		} catch(ApplicationException e){
			fail("Create Application failed: " + e.toString());
		}
		try {
			user = new User();
			Settings.getUserDirectory().loadUserBean(user);
		} catch (DirectoryException e) {
			fail();
			e.printStackTrace();
		}
	}

	/**
	 * Constructor for MultiSelectTest.
	 * @param arg0
	 */
	public MultiSelectTest(String arg0) {
		super(arg0);
	}

	public void testMultiSelectInsert(){
		System.out.println("test multi select insert() ---------------------");
		User user = new User();
		try {
			Settings.getUserDirectory().loadUserBean(user);
		} catch (DirectoryException e) {
			fail();
			e.printStackTrace();
		}
		
		
		FormParameters parameters = new FormParameters();
		parameters.setParentKeyValue(null);
		parameters.setUniqueKeyValue("19");
		parameters.setAction("insert");
		parameters.setUser(user);

		Form theForm = null;
		try {
			theForm = FormFactory.loadForm("TestMultiSelect");
		} catch (FormException e) {
			fail();
		}
		theForm.setFormParameters(parameters);
				
		HashMap keyValues = new HashMap();
		for (Iterator iter = theForm.getFormElements().iterator(); iter.hasNext();) {
			FormElement fe = (FormElement)iter.next();
			if (fe.getDestinationColumn().equals("Value")){
				keyValues.put("Field" + fe.getIdFormElement(), new String[]{"value one", "value two", "value three"});
			} else {
				keyValues.put("Field" + fe.getIdFormElement(), new String[]{"1"});	
			}
			
		}
				
		// TODO: Validation of data
		// DataValidator dv = new DataValidator();
		
		DataModifier dm = new DataModifier();
		try {
			dm.executeFormChange(theForm, keyValues);
		} catch (DataModificationException e) {
			e.printStackTrace();
			fail();
		} catch (DataValidationError e) {
			e.printStackTrace();
			fail();
		}
	}


}
